//
//  LiveTableVc.m
//  Courses
//
//  Created by xxx on 15/1/6.
//  Copyright (c) 2015年 wtf. All rights reserved.
//

#import "LiveTableVc.h"
#import "LiveCell.h"
#import "YYViewController.h"
#import "CoursesViewController.h"
#import "MobClick.h"

#import "LeTVViewController.h"

//#import "ShowYYViewController.h"

#import "PXHGetNetworkStatus.h"
#import "PXHStringComparison.h"
@implementation LiveTableVc

SYNTHESIZE_SINGLETON_FOR_IMPL(LiveTableVc)

#pragma mark - table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.items.count + 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString* identifier = [NSString stringWithFormat:@"cell%ld",(long)indexPath.row];
    LiveCell *cell;
    if (!cell)
    {
        cell = [[LiveCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    if (indexPath.row == self.items.count) {
        UILabel* text = [[UILabel alloc]initWithFrame:AutoRectMake(20, 0, DEFAULT_WIDTH - 50, 40, WTFAutoLayoutTypeText)];
        text.text = @"7x24小时直播";
        text.textColor = [UIColor redColor];
        [cell.contentView addSubview:text];
    }else{
        cell.liveCourse = self.items[indexPath.row];
    }
    return cell;
}

- (NSArray *)fetchedItems
{
    return [LiveCourse getLiveCoursesWithDateIndex:_dateIndex];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [LiveCell liveCellHeight];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == self.items.count) {
        NSLog(@"直播");
        [MobClick event:@"LivePages"];
        YYViewController* yyvc = [[YYViewController alloc]init];
        [[CoursesViewController sharedInstance].navigationController pushViewController:yyvc animated:NO];
    }else{
        NSString* state = [self.items[indexPath.row] liveState];
        if ([PXHGetNetworkStatus isWifi]) {
            if ([state isEqualToString:@"-1"]) {
                NSLog(@"录像");
                [MobClick event:@"PublicPages"];
                LeTVViewController* leTV = [[LeTVViewController alloc] init];
                leTV.user_unique = @"a821022973";
                leTV.video_unique = @"ae05447730";
                [[CoursesViewController sharedInstance].navigationController presentViewController:leTV animated:YES completion:nil];
            }else if([state isEqualToString:@"0"]){
                NSLog(@"直播");
                [MobClick event:@"LivePages"];
                YYViewController* yyvc = [[YYViewController alloc]init];
                [[CoursesViewController sharedInstance].navigationController pushViewController:yyvc animated:NO];
            }else if ([state isEqualToString:@"1"]){
                NSString* message = @"预告节目无法观看";
                ShowTextPromptDelayHide(message, 1, self.view);
            }
        }else{
            UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请在WiFi环境下观看视频！" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
        }
    }
}

@end
